home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
-
- #include "MemoryMadnessC.h"
-
- static void dont_ever_mess_with_the_master_pointer (Handle h) // DONT DO THIS!
- /* Memory Madness point 3 */
- {
- int i;
-
- h = NewHandle( 10 );
- for ( i = 0; i < 10; i++ ) {
- *(*h)++ = 0;
- }
- }
-
- static void dont_know_what_kind_of_handle_we_have () // DONT DO THIS!
- /* Memory Madness point 10 */
- {
- StringHandle h;
-
- h = (StringHandle) GetResource( 'STR ', 128 );
- if ( h == NULL ) {
- h = NewString( "\pHello" );
- }
- // Is h a resource or memory handle
- }
-
- static void know_what_kind_of_handle_we_have ()
- /* Memory Madness point 10 */
- {
- StringHandle h;
-
- h = (StringHandle) GetResource( 'STR ', 128 );
- if ( h == NULL ) {
- h = NewString( "\pHello" );
- } else {
- DetachResource( (Handle) h );
- }
- // use h
- DisposeHandle( (Handle) h );
- }
-
- static void convert_between_memory_and_resource_handles_as_appropriate ()
- /* Memory Madness point 11 */
- {
- int err;
- Handle h;
-
- h = Get1Resource( 'STR ', 128 );
- err = ResError();
- if ( h != NULL ) {
- DetachResource( h );
- AddResource( h, 'STR ', 129 , "\pteststring");
- err = ResError();
- }
- // h is NULL or will be disposed when the resource file is closed
- }
-
-
- static void printstring(Handle h)
- {
- SInt8 state;
-
- state = HGetState(h);
- HLock(h);
- p2cstr((StringPtr) *h);
- printf("%s\n",*h);
- c2pstr(*h);
- HSetState(h, state);
- }
-
- static void demonstratemunger(void)
- /* Memory Madness point 13*/
- {
- OSErr err;
- long where;
- Handle h;
-
- printf("\n");
- h = (Handle) NewString( "\pHello World!" );
-
- printstring(h);
-
- (void) Munger( h, 7, nil, 0, (Ptr) "Cruel ", 6 ); // insert "Cruel "
- err = MemError();
- (**h) = GetHandleSize( h ) - 1; // Correct Pascal String Length;
-
- printstring(h);
-
- DisposeHandle(h);
-
- printf("\n");
- h = (Handle) NewString( "\pHello Cruel World!" );
- (**h) = GetHandleSize( h ) - 1;
-
- printstring(h);
-
- (void) Munger( h, 6, nil, 6, &h, 0 ); // delete "Cruel "
- (**h) = GetHandleSize( h ) - 1;
-
- printstring(h);
-
- DisposeHandle(h);
-
- printf("\n");
- h = (Handle) NewString( "\pHello Cruel World! " );
-
- printstring(h);
-
- where = Munger( h, 0, (Ptr) "Cruel ", 6, (Ptr) "Wonderful ", 10 ); // replace "Cruel " with "Wonderful "
- err = MemError();
- (**h) = GetHandleSize( h ) - 1;
-
- printstring(h);
-
- DisposeHandle(h);
-
- printf("\n");
- h = (Handle) NewString( "\pHello Cruel World!" );
-
- printstring(h);
-
- where = Munger( h, 0, (Ptr) "Cruel ", 6, nil, 0 ); // find "Cruel "
- if ( where >= 0 ) {
- printf( "Found Cruel at offset %ld (remember, offsets are zero based, but pascal strings start with a length)\n", where );
- }
- DisposeHandle(h);
-
- }
-
- pascal void DoCStuff()
- {
- demonstratemunger();
- }
-